package com.akk.platform.manage.business;

import com.akk.common.enums.AdminEnums;
import com.akk.common.enums.AdminRoleEnums;
import com.akk.module.admin.entity.*;
import com.akk.module.admin.model.response.AdminRoleRes;
import com.akk.module.admin.service.AdminMenuService;
import com.akk.module.admin.service.AdminRoleMenuService;
import com.akk.module.admin.service.AdminRoleService;
import com.akk.module.admin.service.AdminService;
import com.akk.module.app.entity.App;
import com.akk.module.app.service.AppService;
import com.akk.utils.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.akk.utils.RestResponse.PLATFORM_ERROR_CODE;


@Service
public class AdminRoleBusiness {

    @Autowired
    AdminRoleService adminRoleService;
    @Autowired
    AdminService adminService;
    @Autowired
    AppService appService;
    @Autowired
    AdminRoleMenuService adminRoleMenuService;
    @Autowired
    AdminMenuService adminMenuService;


    public RestResponse adminRole(String appId,Admin loginAdmin) {
        AdminRole loginAdminRole = adminRoleService.findByRoleId(loginAdmin.getRoleId());
        if(StringUtils.isEmpty(appId)){
            List<AdminRole> adminRoles = adminRoleService.findByRoleTypeAndAppIdIsNullAndFromAdminId(loginAdminRole.getRoleType(),loginAdmin.getAdminId());
            return RestResponse.build(adminRoles);
        }else{
            List<AdminRole> adminRoles = adminRoleService.findByAppIdAndFromAdminId(appId,loginAdmin.getAdminId());
            return RestResponse.build(adminRoles);
        }
    }

    public RestResponse roleList(Query query,Admin loginAdmin) {
        AdminRole adminRole = adminRoleService.findByRoleId(loginAdmin.getRoleId());
        PageHelper.startPage(query.getPage(),query.getLimit());
        Map<String,Object> param = query.getSearch();
        param.put("fromAdminId",loginAdmin.getAdminId());
        List<AdminRoleRes> adminRoles = adminRoleService.findByParam(query.getSearch());
        return RestResponse.build(PageUtils.buildPage(adminRoles,(int) new PageInfo<>(adminRoles).getTotal(),query));
    }

    @Transactional
    public RestResponse roleAdd(AdminRole adminRole) {
        adminRole.setCreateDate(new Date());
        adminRole.setFromAdminId(ShiroUtils.getAdminId());
        adminRole.setRoleId(IDUtils.getPK());
        List<AdminMenu> adminMenus = adminRole.getMenuList();
        AdminMenu rootAdminMenu = adminMenuService.findByMenuId(Constants.PLATFORM_MANAGE_ROOT_MENU_ID);
        adminMenus.add(rootAdminMenu);
        if(ShiroUtils.getAdmin().getAdminType().intValue() == AdminEnums.TypeEnum.OEM.getValue()){
            if(adminRole.getRoleType().intValue() == AdminRoleEnums.TypeEnum.MANAGE.getValue()){
                return RestResponse.build(PLATFORM_ERROR_CODE,"非法操作");
            }else if (adminRole.getRoleType().intValue() == AdminRoleEnums.TypeEnum.OEM.getValue()){
                adminRole.setAppId(ShiroUtils.getAdmin().getAppId());
                adminRoleService.save(adminRole,adminMenus);
                return RestResponse.build("操作成功");
            }
        }
        if(ShiroUtils.getAdmin().getAdminType().intValue() == AdminEnums.TypeEnum.MANAGE.getValue()){
            if(adminRole.getRoleType().intValue() == AdminRoleEnums.TypeEnum.MANAGE.getValue()){
                //创建员工管理角色
                adminRole.setAppId(null);
                adminRoleService.save(adminRole,adminMenus);
                return RestResponse.build("操作成功");
            }else if(!StringUtils.isEmpty(adminRole.getAppId())
                    &&adminRole.getRoleType().intValue() == AdminRoleEnums.TypeEnum.OEM.getValue()){
                //创建OEM管理员
                App app = appService.findByAppId(adminRole.getAppId());
                if(app == null){
                    return RestResponse.build(PLATFORM_ERROR_CODE,"app信息未找到");
                }
                adminRole.setAppId(app.getAppId());
                adminRoleService.save(adminRole,adminMenus);
                return RestResponse.build("操作成功");
            }
        }
        return RestResponse.build(PLATFORM_ERROR_CODE,"操作失败");
    }

    public RestResponse roleMenus(String roleId) {
        AdminRole adminRole = adminRoleService.findByRoleId(roleId);
        if(adminRole==null)
            return RestResponse.build(PLATFORM_ERROR_CODE,"角色不存在");
        List<AdminRoleMenu> roleMenus = adminRoleMenuService.findMenuByRoleId(roleId);
        if(roleMenus.isEmpty()){
            return RestResponse.build(null);
        }
        List<String> menuIds = new ArrayList<>();
        for(AdminRoleMenu roleMenu:roleMenus){
            menuIds.add(roleMenu.getMenuId());
        }
        return RestResponse.build(adminMenuService.findByMenuIds(menuIds));
    }

    public RestResponse roleDel(String roleId, Admin loginAdmin) {
        if(roleId.equals(loginAdmin.getRoleId()))
            return RestResponse.build(PLATFORM_ERROR_CODE,"当前角色不能删除。");
        List<Admin> adminList = adminService.findByRoleId(roleId);
        if(adminList.size()>0)
            return RestResponse.build(PLATFORM_ERROR_CODE,"请先解除角色占用再删除");
        adminRoleMenuService.deleteBatchByRoleId(roleId);
        adminRoleService.deleteByRoleId(roleId);
        return RestResponse.build("角色删除成功");
    }

    @Transactional
    public RestResponse roleUpdate(AdminRole adminRole) {

        List<AdminMenu> newAdminMenus = adminRole.getMenuList();
        AdminRole role = adminRoleService.findByRoleId(adminRole.getRoleId());
        if(role == null)
            return RestResponse.build(PLATFORM_ERROR_CODE,"角色不存在");

        String roleId = role.getRoleId();
        List<AdminRoleMenu> roleMenus = adminRoleService.findRoleMenuByRoleId(roleId);
        List<String> oldMenuIds = new ArrayList<>();
        List<String> newMenuIds = new ArrayList<>();
        oldMenuIds.add(Constants.PLATFORM_MANAGE_ROOT_MENU_ID);
        newMenuIds.add(Constants.PLATFORM_MANAGE_ROOT_MENU_ID);
        for(AdminRoleMenu roleMenu:roleMenus){
            oldMenuIds.add(roleMenu.getMenuId());
        }
        for(AdminMenu menu:newAdminMenus)
            newMenuIds.add(menu.getMenuId());

        for(String menuId:newMenuIds)
            if(!oldMenuIds.contains(menuId)){
                AdminRoleMenu roleMenu = new AdminRoleMenu();
                roleMenu.setRoleMenuId(IDUtils.getPK());
                roleMenu.setMenuId(menuId);
                roleMenu.setRoleId(roleId);
                adminRoleService.saveRoleMenu(roleMenu);
            }
        for (String menuId:oldMenuIds){
            if(! newMenuIds.contains(menuId)){
                adminRoleService.deleteByRoleIdAndMenuId(roleId,menuId);
            }
        }
        role.setRoleName(adminRole.getRoleName());
        role.setRemark(adminRole.getRemark());
        adminRoleService.update(role);
        return RestResponse.build("角色修改成功");
    }

    public RestResponse roleInfo(String roleId) {
        AdminRole role = adminRoleService.findByRoleId(roleId);
        if (role == null){
            return RestResponse.build(new AdminRole());
        }
        List<AdminRoleMenu> roleMenus = adminRoleMenuService.findMenuByRoleId(roleId);
        if(roleMenus.isEmpty()){
            role.setMenuList(null);
            return RestResponse.build(role);
        }
        List<String> menuIds = new ArrayList<>();
        for (AdminRoleMenu roleMenu: roleMenus){
            menuIds.add(roleMenu.getMenuId());
        }
        role.setMenuList(adminMenuService.findByMenuIds(menuIds));
        return RestResponse.build(role);
    }
}
